<?php
class Order {
    public function CreateOrder($token) {
        // get UUID from mySQL
        $id = Order::GetUUID();
        // Session
        $session = $_SESSION['esid'];
        // build sql statement order
        $sql = new SQL();

        $sql->insert[] = "Orders";
        $sql->insert[] = "id, sessionid, token, status";
        $sql->values[] = "'$id', '$session', '$token', 'new'";
        
        $query_order = $sql->ToString($sql);

        // build sql statements items
        $query_items = "INSERT INTO OrderItems
                        SELECT '$id', AccessoryStockCode, AccessoryTitle, AccessoryDescription, AccessoryPrice, Quantity, CURRENT_TIMESTAMP
                        FROM Basket
                        WHERE sessionId = '$session'";

        // execute statements
        DB::query($query_order);
        $invoiceNumber = DB::last_insert();
        DB::query($query_items);
       
        return $invoiceNumber;
    }


    public function EmptySQL() {
        // build sql statement
        $sql = new SQL();

        $sql->delete[] = "Basket";
        $sql->where[] = "SessionID = '" . $_SESSION['esid'] . "'";

        // send to string
        $sql = $sql->ToString($sql);

        return $sql;
    }

    private function GetUUID() {
            $uuid = DB::fetch(DB::query("SELECT UUID() uuid"));
            return $uuid['uuid'];
        }
}
?>
